package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 虎哥
 */
@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /*
    SELECT *
    FROM tb_brand
    WHERE `name` LIKE "%x%" OR letter = "x"
    ORDER BY id DESC
    LIMIT 0, 5
     */
    public PageDTO<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        Example example = new Example(Brand.class);
        // 1.构建查询条件 WHERE `name` LIKE "%x%" OR letter = "x"
        if (StringUtils.isNotBlank(key)) {
            // 不为空，可以查询
            example.createCriteria()
                    .orLike("name", "%" + key + "%") // `name` LIKE "%x%"
                    .orEqualTo("letter", key);// letter = "x"
        }
        // 2.排序条件 ORDER BY id DESC
        example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));
        // 3.分页LIMIT 0, 5
        Page<Brand> result = PageHelper.startPage(page, rows);
        // 4.完成查询
        List<Brand> list = brandMapper.selectByExample(example);
        // 5.返回结果
        List<BrandDTO> items = BeanHelper.copyWithCollection(list, BrandDTO.class);
        return new PageDTO<BrandDTO>(result.getTotal(), result.getPages(), items);
    }

    public BrandDTO findById(Long id) {
        return BeanHelper.copyProperties(brandMapper.selectByPrimaryKey(id), BrandDTO.class);
    }

    public List<BrandDTO> listByIds(List<Long> ids) {
        return BeanHelper.copyWithCollection(brandMapper.selectByIdList(ids), BrandDTO.class);
    }

    public List<BrandDTO> listByCategory(Long id) {
        return BeanHelper.copyWithCollection(brandMapper.queryByCategoryId(id), BrandDTO.class);
    }

    @Transactional
    public void save(BrandDTO brandDTO) {
        // 新增brand
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brandMapper.insertSelective(brand);
        // 新增中间表
        List<Long> categoryIds = brandDTO.getCategoryIds();
        // 批处理
        brandMapper.saveCategoryBrand(categoryIds, brand.getId());
    }

    @Transactional
    public void update(BrandDTO brandDTO) {
        if (brandDTO.getId() == null) {
            throw new LyException(400, "品牌id不能为空！");
        }
        // 删除brand
        brandMapper.deleteByPrimaryKey(brandDTO.getId());
        // 删除中间表
        brandMapper.deleteCategoryBrandOfBrand(brandDTO.getId());
        // 新增中间表
        List<Long> categoryIds = brandDTO.getCategoryIds();
        // 批处理
        brandMapper.saveCategoryBrand(categoryIds, brandDTO.getId());
    }
}